import 'package:app/common/colors.dart';
import 'package:app/common/tool.dart';
import 'package:app/components/base_scaffold_appbar.dart';
import 'package:app/services/address.dart';
import 'package:app/services/dio_manager.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:bruno/bruno.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:get/get.dart';

import '../../../components/custom_footer.dart';
import '../../../components/nodata_page.dart';
class AllOfficeSuppliesPage extends StatefulWidget {
  const AllOfficeSuppliesPage({Key? key}) : super(key: key);

  @override
  State<AllOfficeSuppliesPage> createState() => _AllOfficeSuppliesPageState();
}

class _AllOfficeSuppliesPageState extends State<AllOfficeSuppliesPage> {

  var page = 1;

  var dataArr = [];

  var searchTextEditingController = TextEditingController();
  var easyRefreshController = EasyRefreshController();


  requestData({var search})async{
    var params = {
      'page':page,
      'limit':'100',
      'type':'',
      'search':search,
    };
    var json = await DioManager().kkRequest(Address.allOffice,params:params,method: 'get');

    AllOfficeModel model = AllOfficeModel.fromJson(json);

    if(page == 1){
      easyRefreshController.resetLoadState();
      dataArr.clear();
    }
    if(model.data!=null){
      dataArr.addAll(model.data!);
    }else if(model.data == null){
      BotToast.showText(text: '暂无更多');
      easyRefreshController.finishLoad(noMore: true);
    }
    setState(() {

    });
  }
  onRefresh()async{
    page = 1;
    requestData();
  }
  onLoad()async{
    page++;
    requestData();
  }


  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    requestData();
  }
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: BaseAppBar(
        preferredSize: Size(Get.width, AppTool.appBarHEIGHT),
        child: Text('所有办公用品'),
      ),
      body: Column(
        children: [
          Container(
            height: 55,
            child: BrnSearchText(
              // focusNode: focusNode,
              controller: searchTextEditingController,
              borderRadius: const BorderRadius.all(const Radius.circular(18.0)),
              // searchController: scontroller..isActionShow = true,
              onTextClear: () {
                return false;
              },
              autoFocus: false,
              onActionTap: () {
                // scontroller.isClearShow = false;
                // scontroller.isActionShow = false;
                // focusNode.unfocus();
                // BrnToast.show('取消', context);
              },
              onTextCommit: (text) {
                // BrnToast.show('提交内容 : $text', context);
              },
              onTextChange: (text) {
                // BrnToast.show('输入内容 : $text', context);
                requestData(search: text);
              },
            ),
          ),
          Expanded(child: EasyRefresh.custom(
            controller: easyRefreshController,
            onRefresh: ()=>onRefresh(),
            onLoad: ()=>onLoad(),
            footer: MaterialFooter1(),
            emptyWidget: dataArr.isEmpty?NoDataPage():null,
            slivers: [
              SliverList(
                delegate: _mySliverChildBuilderDelegate(),
              ),
            ],
          )),
        ],
      ),
    );
  }
  _mySliverChildBuilderDelegate() {
    return SliverChildBuilderDelegate(
          (BuildContext context, int index) {
            AllOfficeItemModel model = dataArr[index];
        return GestureDetector(
          onTap: ()async{
            Get.back(result: {'name':model.name,'id':model.id});
          },
          child:Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(
                color: Colors.white,
                width: Get.width,
                padding: EdgeInsets.only(left: 15),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text('名称:${model.name??''}'),
                    Text('数量:${model.number??''}'),
                    Text('型号:${model.model??''}'),
                    Text('类型:${model.type??''}'),
                  ],
                ),
              ),
              SizedBox(height: 5,),
              Container(
                height: 0.5,
                color: AppColor.lineColor,
              )
            ],
          ),
        );
      },
      childCount: dataArr.length,
    );
  }

}

class AllOfficeModel {
  int? errcode;
  String? errmsg;
  List<AllOfficeItemModel>? data;
  String? message;

  AllOfficeModel({this.errcode, this.errmsg, this.data, this.message});

  AllOfficeModel.fromJson(Map<String, dynamic> json) {
    errcode = json['errcode'];
    errmsg = json['errmsg'];
    if (json['data'] != null) {
      data = <AllOfficeItemModel>[];
      json['data'].forEach((v) {
        data!.add(new AllOfficeItemModel.fromJson(v));
      });
    }
    message = json['message'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['errcode'] = this.errcode;
    data['errmsg'] = this.errmsg;
    if (this.data != null) {
      data['data'] = this.data!.map((v) => v.toJson()).toList();
    }
    data['message'] = this.message;
    return data;
  }
}

class AllOfficeItemModel {
  int? id;
  String? name;
  int? number;
  String? model;
  String? type;

  AllOfficeItemModel({this.id, this.name, this.number, this.model, this.type});

  AllOfficeItemModel.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    name = json['name'];
    number = json['number'];
    model = json['model'];
    type = json['type'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['name'] = this.name;
    data['number'] = this.number;
    data['model'] = this.model;
    data['type'] = this.type;
    return data;
  }
}

